-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Vec support for transparent structs #199
base: master
Are you sure you want to change the base?
Conversation
# Conflicts: # crates/swift-bridge-ir/src/codegen/generate_rust_tokens/shared_struct.rs
…ft-bridge into feature/shared-struct-derive
# Conflicts: # crates/swift-bridge-ir/src/codegen/generate_rust_tokens/shared_struct.rs
let vec_map = if shared_struct.derives.copy { | ||
quote! { *v } | ||
} else { | ||
quote! { v.clone() } | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice that if the struct has the Copy
derive we'll opt for using a copy rather than a clone. This whole block is guarded by an if statement checking if can_generate_vec_of_transparent_struct_functions
, which at the moment will only return true if it is a swift_repr = "struct"
with a #[derive(Copy, Clone)]
or #[derive(Clone)]
.
@chinedufn this should be ready to be reviewed once you have the time. Cheers |
Awesome! I'll review this weekend.
I don't think that we can land this part. This means that implicit cloning is unacceptable. I think that we should land the |
Do you think we could put this kind of behavior behind a flag on the crate, so people can explicitly opt for the degraded performance in favor of supporting String fields? |
What's your motivation? I'd rather just implement |
Hey, yeah, so my reasoning is that on most cases About using pointers to pass the values around and not needing to use I was thinking about how to do it, the main issue I've stumbled is that we pass a pointer to the Rust repr to Swift (on current Vec support that is) so Swift doesn't have a way to transform that Pointer into it's own repr, so we would have to start using the FFI repr pointer instead, which both Swift and Rust can understand, but that would mean that we would need to change somehow I might be completely wrong on how to approach this as well. But yeah that was pretty much why I thought would be a good idea to enable the use of |
Gotcha, thanks for explaining. Yeah, agreed that I'll take some time on Tuesday to think through I had some ideas pop into my head earlier today. It might actually be fairly straightforwards.. but I have to sketch it out to be sure.. If it ends up being easy we can
I would say that no matter what we probably won't land clone based Vec support, even behind a feature flag. Happy to land this PR with Copy Otherwise I should have time to design how |
Hey, I think it will be better to just wait until we land the |
Sounds good. Feel free to subscribe to the Just wrote down a design |
# Conflicts: # crates/swift-bridge-ir/src/codegen/generate_c_header.rs
hey @chinedufn I've added a new feature flag to the package here ( If you still don't think this is worth it feel free to close this PR, I'll be using my fork for the time being since I needed this functionality for a project I'm working on. Cheers |
I don't think that a feature flag should land but I'll still keep it open since once class based structs are solved your code will have us most of the way there. My apologies if this is at all frustrating for you. |
No problem at all, will leave it open then. Cheers |
I really need this feature, how far are we from landing class based structs and unblock this @chinedufn? |
Original PR was #193, but I had decided to open a PR from a feature branch rather than from
master
from my fork.This PR addresses #91
Added all the shims so transparent structs can support Vec. A few caveats though:
swift_repr = "class"
are not supported, this PR only add support toswift_repr = "struct"
Vec
support to actually be generated, the struct must be annotated with#[derive(Copy, Clone)]
Vec
support is also generated if the struct is annotated with only#[derive(Clone)]
, this is useful for structs with fields that cannot implementCopy
(likeString
), however there is a performance overhead of usingClone
rather thanCopy
, so it is advised to include#[derive(Copy)]
as well.Vec
support to be generated, you MUST use the#[derive]
macro, implementing the trait withimpl Copy for Struct {}
won't generate theVec
support code.Here is an example of the Rust code:
And then on Swift you can: